<?php

use Illuminate\Database\Seeder;

class MenusTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $menus = json_decode(file_get_contents(database_path('data/menu.json')), true);
        $data = [];

        foreach ($menus as $menu) {
            if (isset($menu['children'])) {
                $children = $menu['children'];
                unset($menu['children']);
                $data[] = $menu;
                foreach ($children as $subMenu) {
                    $data[] = $subMenu;
                }
            } else {
                $data[] = $menu;
            }
        }

        foreach ($data as &$val) {
            $val['created_at'] = now();
            $val['updated_at'] = now();
        }

        \App\Models\Menu::insert($data);
    }
}
